<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thuan
 * Date: 7/1/14
 * Time: 10:20 AM
 * To change this template use File | Settings | File Templates.
 */

namespace Goxob;


class Helper {
    public static function ipToCountry($ip) {
        $two_letter_country_code = '';

        $numbers = preg_split( "/\./", $ip);
        $ranges=Array( "985268224" => array("985399295","VN"), "1063573680" => array("1063573695","VN"), "2111176704" => array("2111193087","VN"), "2112487424" => array("2112618495","VN"), "3389391360" => array("3389391615","VN"), "3389415424" => array("3389415935","VN"), "3392114176" => array("3392114431","VN"), "3392954368" => array("3392958463","VN"), "3394166784" => array("3394168831","VN"), "3394234368" => array("3394236415","VN"), "3397783552" => array("3397785599","VN"), "3398934528" => array("3398938623","VN"), "3400440960" => array("3400441023","VN"), "3410866688" => array("3410866943","VN"), "3413213184" => array("3413229567","VN"), "3413575680" => array("3413576703","VN"), "3413582848" => array("3413583871","VN"), "3413584896" => array("3413585919","VN"), "3414224896" => array("3414226943","VN"), "3416260608" => array("3416261119","VN"), "3416285184" => array("3416289279","VN"), "3416326144" => array("3416327167","VN"), "3416391680" => array("3416457215","VN"), "3418267648" => array("3418271743","VN"), "3418294272" => array("3418296319","VN"), "3419570176" => array("3419602943","VN"), "3539271680" => array("3539304447","VN"), "3663989248" => array("3663989503","VN"), "3663990272" => array("3663990527","VN"), "3706142720" => array("3706159103","VN"), "3715694592" => array("3715710975","VN"), "3716415488" => array("3716431871","VN"), "3716481024" => array("3716489215","VN"), "3741057024" => array("3741319167","VN"), );
        $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
        foreach($ranges as $key => $value){
            if($key<=$code){
                if($ranges[$key][0]>=$code){
                    $two_letter_country_code=$ranges[$key][1];break;
                }
            }
        }
        if ($two_letter_country_code==""){
            $two_letter_country_code="Not VN";
        }
        return $two_letter_country_code;
    }

    public static function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
        $output = NULL;
        if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
            $ip = $_SERVER["REMOTE_ADDR"];
            if ($deep_detect) {
                if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                    $ip = $_SERVER['HTTP_CLIENT_IP'];
            }
        }
        $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
        $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
        $continents = array(
            "AF" => "Africa",
            "AN" => "Antarctica",
            "AS" => "Asia",
            "EU" => "Europe",
            "OC" => "Australia (Oceania)",
            "NA" => "North America",
            "SA" => "South America"
        );
        if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
            $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
            if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
                switch ($purpose) {
                    case "location":
                        $output = array(
                            "city"           => @$ipdat->geoplugin_city,
                            "state"          => @$ipdat->geoplugin_regionName,
                            "country"        => @$ipdat->geoplugin_countryName,
                            "country_code"   => @$ipdat->geoplugin_countryCode,
                            "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                            "continent_code" => @$ipdat->geoplugin_continentCode
                        );
                        break;
                    case "address":
                        $address = array($ipdat->geoplugin_countryName);
                        if (@strlen($ipdat->geoplugin_regionName) >= 1)
                            $address[] = $ipdat->geoplugin_regionName;
                        if (@strlen($ipdat->geoplugin_city) >= 1)
                            $address[] = $ipdat->geoplugin_city;
                        $output = implode(", ", array_reverse($address));
                        break;
                    case "city":
                        $output = @$ipdat->geoplugin_city;
                        break;
                    case "state":
                        $output = @$ipdat->geoplugin_regionName;
                        break;
                    case "region":
                        $output = @$ipdat->geoplugin_regionName;
                        break;
                    case "country":
                        $output = @$ipdat->geoplugin_countryName;
                        break;
                    case "countrycode":
                        $output = @$ipdat->geoplugin_countryCode;
                        break;
                }
            }
        }
        return $output;
    }

}